package com.xjgc.eip.panel.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjgc.common.utils.DateUtil;
import com.xjgc.common.utils.PushDataUtil;
import com.xjgc.common.utils.TransNoUitl;
import com.xjgc.eip.panel.entity.ZxjEipXsdd;
import com.xjgc.eip.panel.mapper.ZxjEipXsddMapper;
import com.xjgc.eip.panel.service.ZxjEipXsddService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanghongbin
 * @since 2019-11-17
 */
@Service
@DS("proxyDs")
public class ZxjEipXsddServiceImpl extends ServiceImpl<ZxjEipXsddMapper, ZxjEipXsdd> implements ZxjEipXsddService {
    @Value("${xsddUrl}")
    public String url;
    @Autowired
    private ZxjEipXsddMapper zxjEipXsddMapper;
    @Transactional
    public void salesOrder(){
        List<ZxjEipXsdd> list=zxjEipXsddMapper.selectSoNo();
        String orisys="0";
        String transno=TransNoUitl.genTransNo();
        String operatetype="add";
        for (ZxjEipXsdd zes:list) {
            QueryWrapper<ZxjEipXsdd> itemQueryWrapper = new QueryWrapper<>();
            itemQueryWrapper.ne("ioflag","0");
            itemQueryWrapper.eq("so_no",zes.getSoNo());
            List<ZxjEipXsdd> salesOrderList=this.list(itemQueryWrapper);
            if(salesOrderList!=null&&salesOrderList.size()>0){
                for (ZxjEipXsdd zex:salesOrderList) {
                    if ("1".equals(zex.getStatus())){
                        operatetype="update";
                    }
                    //body数据  就是zex
                    //String url="http://10.10.7.70:22108/api/supplier-so";
                    net.sf.json.JSONObject json= net.sf.json.JSONObject.fromObject(zex);
                    json.put("itemList","[]");
                    String returnString= PushDataUtil.pushData(url,orisys,transno,operatetype,zex);
                    if(returnString==null){
                        System.out.println("=======================》》》》》》》》》》无返回数据");
                        return;
                    }else{
                        JSONObject jsonObject=JSONObject.parseObject(returnString);
                        String status=jsonObject.getString("status");
                        String message=jsonObject.getString("message");
                        if("0".equals(status)||"00000".equals(status)||"10000".equals(status)){
                            zex.setIoflag("0");
                            zex.setMessage("入库成功");
                            zes.setPushTime(new Date());
                            this.updateById(zex);
                        }else{
                            zex.setIoflag(status);
                            zex.setMessage(message);
                            zes.setPushTime(new Date());
                            this.updateById(zex);
                        }

                    }
                }
            }

//            ZxjEipXsdd zxjEipXsddItem=itemList.get(0);
//            net.sf.json.JSONObject json= net.sf.json.JSONObject.fromObject(zxjEipXsddItem);
//            json.put("itemList",itemList);
//            JSONArray jsonArray=json.getJSONArray("itemList");
//            JSONArray itemArray=new JSONArray();
//            for (int i = 0; i <jsonArray.size(); i++) {
//                Object object=jsonArray.get(i);
//                net.sf.json.JSONObject jsonEntiy=net.sf.json.JSONObject.fromObject(object);
//                jsonEntiy.put("eipPoNo","");
//                itemArray.add(jsonEntiy);
//            }
//            json.put("itemList",itemArray);
            //header数据

        }
    }

    @Override
    public Map selectByTime(Map map) {
        return zxjEipXsddMapper.selectByTime(map);
    }

    @Override
    public Map selectByAreaTime() throws ParseException {
        //统计map
        Map allMap=new HashMap();
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        Date nowDate=new Date();
        String nowDateString=format.format(nowDate);
        Integer nowDateDay=Integer.parseInt(nowDateString.substring(nowDateString.length()-2));
        Date startDate=null;
        if(nowDateDay>6){
            Integer nowWeakDay=Integer.parseInt(nowDateString);
            Integer firstWeakDay=(nowWeakDay-6);
            startDate=format.parse(firstWeakDay.toString());
        }else {
            startDate=DateUtil.getBeginDayOfMonth();
        }

        HashMap weakMap=new HashMap();
        weakMap.put("startTime",startDate);
        weakMap.put("endTime",nowDate);
        List<Map> oneWeakMap=zxjEipXsddMapper.selectByAreaTime(weakMap);

        Map<String,List<Map>>dataMap=new LinkedHashMap<>();
        for (Map map:oneWeakMap){
            if(dataMap.get(map.get("push_time"))!=null){
                dataMap.get(map.get("push_time")).add(map);
            }else{
                if(map!=null){
                    List<Map> list=new ArrayList<>();
                    list.add(map);
                    dataMap.put(map.get("push_time").toString(),list);
                }

            }
        }

//        //周统计 第一周
//        Date fistDate=DateUtil.getBeginDayOfMonth();
//        String firstDayString=format.format(fistDate);
//        int firstDayInt=Integer.parseInt(firstDayString);
//        Integer firstWeak=firstDayInt+6;
//        Date firtWeakDate=format.parse(firstWeak.toString());
//        //山东电子
//        HashMap weakMap=new HashMap();
//        weakMap.put("startTime",fistDate);
//        weakMap.put("endTime",firtWeakDate);
//        List<Map> firstWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//        allMap.put("firstWeak",firstWeakReturnMap);
//        Integer secondWeak=0;
//        Integer secondWeakLast=0;
//        Integer threeWeakLast=0;
//        Integer fourWeakLast=0;
//        if (nowDateDay.intValue()>7) {
//            //第二周
//            secondWeak=firstWeak+1;
//            Date firstOfSecondWeakDate=format.parse(secondWeak.toString());
//            secondWeakLast=secondWeak+6;
//            Date lastOfSecondWeakDate=format.parse(secondWeakLast.toString());
//            weakMap.put("startTime",firstOfSecondWeakDate);
//            weakMap.put("endTime",lastOfSecondWeakDate);
//            List<Map> secondWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("secondWeak",secondWeakReturnMap);
//        }
//        if (nowDateDay.intValue()>14) {
//            //第三周
//            Integer threeWeak=secondWeakLast+1;
//            Date firstOfThreeWeakDate=format.parse(threeWeak.toString());
//            threeWeakLast=threeWeak+6;
//            Date lastOfThreeWeakDate=format.parse(threeWeakLast.toString());
//            weakMap.put("startTime",firstOfThreeWeakDate);
//            weakMap.put("endTime",lastOfThreeWeakDate);
//            List<Map> threeWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("threeWeak",threeWeakReturnMap);
//        }
//        if (nowDateDay.intValue()>20) {
//            //第四周
//            Integer fourWeak=threeWeakLast+1;
//            Date firstOfFourWeakDate=format.parse(fourWeak.toString());
//            fourWeakLast=fourWeak+6;
//            Date lastOfFourWeakDate=format.parse(fourWeakLast.toString());
//            weakMap.put("startTime",firstOfFourWeakDate);
//            weakMap.put("endTime",lastOfFourWeakDate);
//            List<Map> fourWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("fourWeak",fourWeakReturnMap);
//        }
//        if(nowDateDay.intValue()>27){
//            //第五周
//            Integer fiveWeak=fourWeakLast+1;
//            Date firstOfFiveWeakDate=format.parse(fiveWeak.toString());
//            Date lastOfMonthDay=DateUtil.getEndDayOfMonth();
//            weakMap.put("startTime",firstOfFiveWeakDate);
//            weakMap.put("endTime",lastOfMonthDay);
//            List<Map> fiveWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("fiveWeak",fiveWeakReturnMap);
//        }
        return dataMap;
    }

    @Override
    public Map selectByMonth() throws ParseException {
        //统计map
        Map allMap=new HashMap();
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        Date nowDate=new Date();
        String nowDateString=format.format(nowDate);
        Integer nowDateDay=Integer.parseInt(nowDateString.substring(nowDateString.length()-2));
        //周统计 第一周
        Date fistDate=DateUtil.getBeginDayOfMonth();
        String firstDayString=format.format(fistDate);
        int firstDayInt=Integer.parseInt(firstDayString);
        Integer firstWeak=firstDayInt+6;
        Date firtWeakDate=format.parse(firstWeak.toString());
        Map weakMap=new LinkedHashMap();
        weakMap.put("startTime",fistDate);
        weakMap.put("endTime",firtWeakDate);
        List<Map> firstWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
        allMap.put("firstWeak",firstWeakReturnMap);
        Integer secondWeak=0;
        Integer secondWeakLast=0;
        Integer threeWeakLast=0;
        Integer fourWeakLast=0;
        if (nowDateDay.intValue()>7) {
            //第二周
            secondWeak=firstWeak+1;
            Date firstOfSecondWeakDate=format.parse(secondWeak.toString());
            secondWeakLast=secondWeak+6;
            Date lastOfSecondWeakDate=format.parse(secondWeakLast.toString());
            weakMap.put("startTime",firstOfSecondWeakDate);
            weakMap.put("endTime",lastOfSecondWeakDate);
            List<Map> secondWeakReturnMap=zxjEipXsddMapper.selectByMonth(weakMap);
            allMap.put("secondWeak",secondWeakReturnMap);
        }
        if (nowDateDay.intValue()>14) {
            //第三周
            Integer threeWeak=secondWeakLast+1;
            Date firstOfThreeWeakDate=format.parse(threeWeak.toString());
            threeWeakLast=threeWeak+6;
            Date lastOfThreeWeakDate=format.parse(threeWeakLast.toString());
            weakMap.put("startTime",firstOfThreeWeakDate);
            weakMap.put("endTime",lastOfThreeWeakDate);
            List<Map> threeWeakReturnMap=zxjEipXsddMapper.selectByMonth(weakMap);
            allMap.put("threeWeak",threeWeakReturnMap);
        }
        if (nowDateDay.intValue()>20) {
            //第四周
            Integer fourWeak=threeWeakLast+1;
            Date firstOfFourWeakDate=format.parse(fourWeak.toString());
            fourWeakLast=fourWeak+6;
            Date lastOfFourWeakDate=format.parse(fourWeakLast.toString());
            weakMap.put("startTime",firstOfFourWeakDate);
            weakMap.put("endTime",lastOfFourWeakDate);
            List<Map> fourWeakReturnMap=zxjEipXsddMapper.selectByMonth(weakMap);
            allMap.put("fourWeak",fourWeakReturnMap);
        }
        if(nowDateDay.intValue()>27){
            //第五周
            Integer fiveWeak=fourWeakLast+1;
            Date firstOfFiveWeakDate=format.parse(fiveWeak.toString());
            Date lastOfMonthDay=DateUtil.getEndDayOfMonth();
            weakMap.put("startTime",firstOfFiveWeakDate);
            weakMap.put("endTime",lastOfMonthDay);
            List<Map> fiveWeakReturnMap=zxjEipXsddMapper.selectByMonth(weakMap);
            allMap.put("fiveWeak",fiveWeakReturnMap);
        }
        return allMap;
    }

    @Override
    public List<Map> selectByJiDu() {
        Integer year=DateUtil.getNowYear();
        Integer endMonth=DateUtil.getNowMonth();
        Map weakMap=new LinkedHashMap();
        weakMap.put("startTime",year+"-"+(endMonth-2));
        weakMap.put("endTime",endMonth);
        List<Map> fiveWeakReturnMap=zxjEipXsddMapper.selectByJiDu(weakMap);
        return fiveWeakReturnMap;
    }

    @Override
    public Map selectByYear() {
        Integer year=DateUtil.getNowYear();
        Integer endMonth=DateUtil.getNowMonth();
        Map<String,List<Map>> map=new LinkedHashMap<>();
        if(endMonth<3){
            Map weakMap=new HashMap();
            weakMap.put("startTime",year+"-"+"01");
            weakMap.put("endTime",endMonth);
            List<Map> jiDuMap=zxjEipXsddMapper.selectByYear(weakMap);
            map.put("第一季度",jiDuMap);
        }else{
            if(endMonth>3&& endMonth<=6){
                Map weakMap=new HashMap();
                weakMap.put("startTime",year+"-"+"04");
                weakMap.put("endTime",endMonth);
                List<Map> jiDuMap=zxjEipXsddMapper.selectByYear(weakMap);
                map.put("第二季度",jiDuMap);
            }
            if(endMonth>6&& endMonth<=9){
                Map weakMap=new HashMap();
                weakMap.put("startTime",year+"-"+"07");
                weakMap.put("endTime",endMonth);
                List<Map> jiDuMap=zxjEipXsddMapper.selectByYear(weakMap);
                map.put("第三季度",jiDuMap);
            }
            if(endMonth>9&& endMonth<=12){
                Map weakMap=new HashMap();
                weakMap.put("startTime",year+"-"+"10");
                weakMap.put("endTime",endMonth);
                List<Map> jiDuMap=zxjEipXsddMapper.selectByYear(weakMap);
                map.put("第四季度",jiDuMap);
            }
        }
        return map;
    }

    public static void main(String[] args) {
//
        String str="20191219";
        str=str.substring(str.length()-2);
        System.out.println(Integer.parseInt(str));
    }
}
